﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.EntityClient;

namespace NMigrate
{
	public class EntityContextHelper
	{
		public static T CreateContext<T>(string baseConnectionString, string provider = null, string resourceName = null)
		{
			if(resourceName == null)
				resourceName = typeof(T).Namespace.Remove(0, typeof(T).Assembly.GetName().Name.Length + 1) + "." + typeof(T).Name;
			if(provider == null)
				provider = "System.Data.SqlClient";
			EntityConnectionStringBuilder builder = new EntityConnectionStringBuilder();
			builder.ProviderConnectionString = baseConnectionString;
			builder.Provider = provider;
			builder.Metadata = String.Format("res://{0}/{1}.csdl|res://{0}/{1}.ssdl|res://{0}/{1}.msl",
				typeof(T).Assembly.FullName, resourceName);
			return (T)Activator.CreateInstance(typeof(T), builder.ToString());

		}
	}
}
